/**
 * 
 */
package xj.toolkit.fsm.util;

import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author hp
 *
 */
public class UUID2IntDictionary {
	
	private	Map<UUID, Integer>	uuid2int = 
		new ConcurrentHashMap<UUID, Integer>();
	
	private	Map<Integer, UUID>	int2uuid = 
		new ConcurrentHashMap<Integer, UUID>();
	
	private	AtomicInteger	idx = new AtomicInteger(0);
	
	public	int	allocInt(UUID id) {
		int  ret = idx.getAndIncrement();
		UUID  cloneId = new UUID(id.getMostSignificantBits(), id.getLeastSignificantBits());
		
		uuid2int.put(cloneId, ret);
		int2uuid.put(ret, cloneId);
		
		return	ret;
	}
	
	public UUID queryUUID(int intValue) {
		return	int2uuid.get(intValue);
	}
	
	public	void removeByUUID(UUID id) {
		Integer intValue = uuid2int.get(id);
		
		uuid2int.remove(id);
		
		if ( null != intValue ) {
			int2uuid.remove(intValue);
		}
	}
}
